home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_07 / guthrie / aqtest.cpp < prev    next >
C/C++ Source or Header  |  1994-10-06  |  6KB  |  137 lines

  1. //========================= AQTEST.CPP ==================================
  2. //  AutoQueue Class Test and Example File.
  3. //  This presents a series of examples for the use of AutoQueueMessage.
  4. //=======================================================================
  5. #include "AQMSG.HPP"
  6. #include <iostream.h>
  7.  
  8. // Local Function Prototype
  9. void ListMessageQueues(AutoQueueMessage* List1, AutoQueueMessage* List2);
  10.  
  11. void main()
  12. {
  13.   AutoQueueMessage* pMessage = 0;
  14.   AutoQueueMessage* NonLinked;
  15.   AutoQueueMessage* NonLinked2;
  16.  
  17.   // -- Demonstrate stand alone (non-linked) instance of AutoQueueMessage --
  18.   cout << "CREATE NON-LINKED INSTANCE and show its contents." << endl;
  19.   NonLinked = new AutoQueueMessage(6, (string)"Hello World!");
  20.   if(NonLinked)           // Make sure NonLinked exists.
  21.     NonLinked->List();    // List contents of this non-linked instance.
  22.   else
  23.     cout << "The Non-Linked instance didn't create..." << endl;
  24.   cout << endl;           // Add a blank line separator to output listing.
  25.  
  26.   // -- Copy previously created non-linked entry to a new stand alone entry --
  27.   if(NonLinked)
  28.   { cout << "COPY STAND ALONE ENTRY to STAND ALONE ENTRY." << endl;
  29.     NonLinked2 = new AutoQueueMessage(NonLinked);
  30.     if(NonLinked2)        // If it created, show contents of both.
  31.     { cout << "  Source Instance: ";
  32.       NonLinked->List();  // List of source.
  33.       cout << "  New Instance   : ";
  34.       NonLinked2->List(); // List of new entry.
  35.     }
  36.     else
  37.       cout << "New stand alone entry failed to create..." << endl;
  38.  
  39.     // To effect a MOVE operation, include a delete of the source instance.
  40.     delete NonLinked;
  41.   }
  42.   cout << endl;
  43.  
  44.   // -- CREATE 2 QUEUES --
  45.   // Allocate Message list pointers. (Initialize to 0)
  46.   AutoQueueMessage* pAQM_List1 = 0;
  47.   AutoQueueMessage* pAQM_List2 = 0;
  48.  
  49.   // -- ADD NODES to a QUEUE --
  50.   cout << "ADD NODES - Add 4 instances to List 1, none to list 2." << endl;
  51.   (void) new AutoQueueMessage(&pAQM_List1, 1, (string)"Message 1");
  52.   (void) new AutoQueueMessage(&pAQM_List1, 2, (string)"Message 2");
  53.   (void) new AutoQueueMessage(&pAQM_List1, 3, (string)"Message 3");
  54.   (void) new AutoQueueMessage(&pAQM_List1, 4, (string)"Message 4");
  55.   ListMessageQueues(pAQM_List1, pAQM_List2);      // Show Queues
  56.  
  57.   // -- COPY STAND ALONE NODE to a QUEUE --
  58.   cout << "COPY STAND ALONE ENTRY to List 2." << endl;
  59.   (void) new AutoQueueMessage(&pAQM_List2, NonLinked2);
  60.   ListMessageQueues(pAQM_List1, pAQM_List2);      // Show Queues
  61.  
  62.   // -- COPY QUEUE NODE to STAND ALONE ENTRY --
  63.   // Find record in pAQM_List1 whos Number_ value is 4 and COPY
  64.   // it to a stand alone instance.
  65.   cout << "COPY NODE From List 1 to a STAND ALONE instance where Number_ = 4."
  66.        << endl;
  67.   if(pAQM_List1)                                       // If entries,
  68.     if((pMessage = pAQM_List1->FindByNumber(4)) != 0)  // find match and
  69.     { NonLinked = new AutoQueueMessage(pMessage);      // create copy.
  70.       if(NonLinked)                                    // If it worked,
  71.       { cout << "  New Instance: ";
  72.         NonLinked->List();                             // List of new entry.
  73.         cout << endl;
  74.       }
  75.     }
  76.  
  77.   // -- COPY QUEUE NODE to ANOTHER QUEUE --
  78.   // Find record in pAQM_List1 whos Number_ value is 2 and COPY to pAQM_List2.
  79.   cout << "COPY NODE From List 1 to List 2 where Number_ = 2." << endl;
  80.   if(pAQM_List1)                                          // If entries,
  81.     if((pMessage = pAQM_List1->FindByNumber(2)) != 0)     // find match and
  82.       (void) new AutoQueueMessage(&pAQM_List2, pMessage); // create copy.
  83.   ListMessageQueues(pAQM_List1, pAQM_List2);      // Show Queues
  84.  
  85.   // -- MOVE NODE from ONE QUEUE to ANOTHER QUEUE --
  86.   // Find record in pAQM_List1 whos Number_ value is 3 and MOVE to pAQM_List2.
  87.   cout << "MOVE NODE From List 1 to List 2 where Number_ = 3." << endl;
  88.   if(pAQM_List1)                                          // If entries,
  89.     if((pMessage = pAQM_List1->FindByNumber(3)) != 0)     // find match
  90.     { // Create new message.
  91.       (void) new AutoQueueMessage(&pAQM_List2, pMessage); // make copy and
  92.       delete pMessage;                                    // delete original.
  93.     }
  94.   ListMessageQueues(pAQM_List1, pAQM_List2);              // Show Queues
  95.  
  96.   // -- DELETE NODE from a QUEUE --
  97.   // Delete the record in List 1 whos Number_ value = 1.
  98.   cout << "DELETE NODE From List 1 where Number_ = 1" << endl;
  99.   if(pAQM_List1)                                          // If entries
  100.     if((pMessage = pAQM_List1->FindByNumber(1)) != 0)     // find match
  101.       delete pMessage;                                    // and delete.
  102.   ListMessageQueues(pAQM_List1, pAQM_List2);              // Show Queues
  103.  
  104.   // -- EMPTY a QUEUE --
  105.   cout << "EMPTY LIST  (Empty both lists)" << endl;
  106.   while(pAQM_List1)                            // While an entry exists,
  107.     delete pAQM_List1;                         // delete it.
  108.  
  109.   // Empty List 2
  110.   while(pAQM_List2)                            // While an entry exists,
  111.     delete pAQM_List2;                         // delete it.
  112.   ListMessageQueues(pAQM_List1, pAQM_List2);   // Show Queues
  113.  
  114.   // Delete stand alone instances.
  115.   delete NonLinked;
  116.   delete NonLinked2;
  117. }  // end main
  118.  
  119. // ===== ListMessageQueues =====
  120. //  List the contents of both AutoQueueMessage lists.
  121. void ListMessageQueues(AutoQueueMessage* List1, AutoQueueMessage* List2)
  122. {
  123.   // -- DISPLAY CONTENTS of both QUEUES --
  124.   cout << "----- List 1 -----" << endl;    // Title for List 1
  125.   if(List1)                                // If elements exist,
  126.     List1->List();                         //   Call list routine.
  127.   else                                     // else
  128.     cout << "  <list is empty>" << endl;   //   Let user know list is empty.
  129.   cout << "----- List 2 -----" << endl;    // Title for List 2
  130.   if(List2)                                // If elements exist,
  131.     List2->List();                         //   Call list routine.
  132.   else                                     // else
  133.     cout << "  <list is empty>" << endl;   //   Let user know list is empty.
  134.   cout << endl;                            // Trailer.
  135. }
  136. // end file AQTEST.CPP
  137.